package com.kexin.jbsg.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;

import org.codehaus.jackson.map.ObjectMapper;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;

/**
 * @author oven:
 * @version 创建时间：2013-3-12 下午2:49:00
 * 
 */
public class Util {
	
    private static ObjectMapper objectMapper = new ObjectMapper();
    static{
    	objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
    }

	/**
	 * 使用对象进行json反序列化。
	 * @param json json串
	 * @param pojoClass 类类型
	 * @return
	 * @throws Exception
	 */
	public static Object decodeJson(String json, Class pojoClass){		
		try{
			return objectMapper.readValue(json, pojoClass);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
		
	public static ProgressDialog showProgressDialog(Context context,String messgae){
		ProgressDialog dialog = ProgressDialog.show(context, "", messgae, true);
        dialog.setCancelable(false);
        return dialog;
	}
	
	
	public static AlertDialog showAlertDialog(Context context,String title,String message){
		Builder builder = new AlertDialog.Builder(context)
				.setTitle(title)
				.setMessage(message)
				.setPositiveButton("确定", null);
		AlertDialog dialog = builder.create();
		dialog.show();
		return dialog;
	}
	
	/**
     * get 请求
     * @param domain
     * @return
     * @throws RequestErrorException
     */
    public static String getWebCon(String domain){
		StringBuffer sb = new StringBuffer();
		try {
			java.net.URL url = new java.net.URL(domain);
			BufferedReader in = new BufferedReader(new InputStreamReader(url
					.openStream(),"UTF-8"));
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			System.out.println(sb.toString());
			in.close();
		} catch (Exception e) { // Report any errors that arise
			sb.append(e.toString());
			System.err.println(e);
		}
		return sb.toString();
	}
    
    
    /** 
     * MD5 加密 
     */  
    public static String getMD5Str(String str) {  
        MessageDigest messageDigest = null;  
        try {  
            messageDigest = MessageDigest.getInstance("MD5");  
  
            messageDigest.reset();  
  
            messageDigest.update(str.getBytes("UTF-8"));  
        } catch (NoSuchAlgorithmException e) {  
            System.out.println("NoSuchAlgorithmException caught!");  
            System.exit(-1);  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
  
        byte[] byteArray = messageDigest.digest();  
  
        StringBuffer md5StrBuff = new StringBuffer();  
  
        for (int i = 0; i < byteArray.length; i++) {              
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)  
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));  
            else  
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));  
        }  
  
        return md5StrBuff.toString();  
    }  
    
//    public static Object decodeJson(Object entityData,Class decodeClass){
//    	String result = null;
//    	JSONObject o = new JSONObject(result);
//    	o.
//    }
}

